﻿using System;
namespace Kis.Xroad.Eu {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="ComplexTypeWithThreeBasicTypes", Namespace="http://kis.x-road.eu/", Target=typeof(ComplexTypeWithThreeBasicTypes))]
    public class ComplexTypeWithThreeBasicTypesSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected ComplexTypeWithThreeBasicTypes clazz;
        
        public ComplexTypeWithThreeBasicTypesSerializer(ComplexTypeWithThreeBasicTypes clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXmlAttributes(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXmlAttributes(Writer);
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("Builtin");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdDateTime(((System.DateTime)(clazz.Builtin))).ToString());
            Writer.WriteEndElement();
            Writer.WriteStartElement("Udf");
            if (clazz.Udf!=null) {
                Writer.WriteString(clazz.Udf.ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Udf");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("Complex");
            if (clazz.Complex!=null) {
                global::Xtee.Core.SerializationMetadata metaComplex = this.GetMetaData(clazz.Complex);
                if (ShouldWriteTypeAttribute(typeof(global::Kis.Xroad.Eu.Employee), clazz.Complex.GetType())) {
                    Writer.WriteTypeAttribute(metaComplex.TypeQualifiedName.Namespace, metaComplex.TypeQualifiedName.Name);
                }
                var complexSerializer = metaComplex.NewSerializer(clazz.Complex);
                complexSerializer.WriteXmlAttributes(Writer);
                complexSerializer.WriteXml(Writer);
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Complex");
            }
            Writer.WriteEndElement();
        }
        
        public override void ReadXmlAttributes(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXmlAttributes(Reader);
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="Builtin") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>ComplexTypeWithThreeBasicTypes>Builtin");
                }
                clazz.Builtin = new global::Xtee.Core.Schema.XsdDateTime(Reader.ReadElementString("Builtin")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>ComplexTypeWithThreeBasicTypes>Builtin", Reader.LocalName);
            }
            if (Reader.LocalName=="Udf") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>ComplexTypeWithThreeBasicTypes>Udf");
                }
                clazz.Udf = new global::Kis.Xroad.Eu.Gender(Reader.ReadElementString("Udf"));
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>ComplexTypeWithThreeBasicTypes>Udf", Reader.LocalName);
            }
            if (Reader.LocalName=="Complex") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>ComplexTypeWithThreeBasicTypes>Complex");
                }
                global::Xtee.Core.SerializationMetadata metaComplex = this.GetMetaData<global::Kis.Xroad.Eu.Employee>(Reader, "Employee", "http://kis.x-road.eu/");
                clazz.Complex = metaComplex.NewEntity<global::Kis.Xroad.Eu.Employee>();
                if ((Reader.IsEmptyElement == false)) {
                    var complexSerializer = metaComplex.NewSerializer(clazz.Complex);
                    complexSerializer.ReadXmlAttributes(Reader);
                    Reader.ReadStartElement("Complex");
                    complexSerializer.ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>ComplexTypeWithThreeBasicTypes>Complex", Reader.LocalName);
            }
        }
    }
}
